package jogodavelhagui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Felipe
 */
public class JogoDaVelhaGUI extends javax.swing.JFrame {
    
    private Campo campo = new Campo();
    private Jogada jogada = new Jogada();
    String labelJ1 = "Jogadas: ", lJog1 = "Jogador 1: ", lJog2 = "Jogador 2: ", lEmpate = "Empates:  ";
    int Jog1=0, Jog2=0, Empate=0;
    
    public void wasVictorious() {
        button00.setEnabled(false);
        button01.setEnabled(false);
        button02.setEnabled(false);
        button10.setEnabled(false);
        button11.setEnabled(false);
        button12.setEnabled(false);
        button20.setEnabled(false);
        button21.setEnabled(false);
        button22.setEnabled(false);
        labelJogador1.setText(lJog1 + Jog1);
        labelJogador2.setText(lJog2 + Jog2);
        labelEmpate.setText(lEmpate + Empate);
    }    
    
    /**
     * Creates new form JogoDaVelhaGUI
     */
    public JogoDaVelhaGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonInicia = new javax.swing.JButton();
        labelJogadas = new javax.swing.JLabel();
        button00 = new javax.swing.JButton();
        button02 = new javax.swing.JButton();
        button01 = new javax.swing.JButton();
        button10 = new javax.swing.JButton();
        button11 = new javax.swing.JButton();
        button20 = new javax.swing.JButton();
        button12 = new javax.swing.JButton();
        button22 = new javax.swing.JButton();
        button21 = new javax.swing.JButton();
        labelJogador1 = new javax.swing.JLabel();
        labelJogador2 = new javax.swing.JLabel();
        labelEmpate = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonInicia.setText("Iniciar Nova Partida");
        buttonInicia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonIniciaActionPerformed(evt);
            }
        });

        labelJogadas.setText("Jogadas: ");

        button00.setPreferredSize(new java.awt.Dimension(75, 75));
        button00.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button00ActionPerformed(evt);
            }
        });

        button02.setPreferredSize(new java.awt.Dimension(75, 75));
        button02.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button02ActionPerformed(evt);
            }
        });

        button01.setPreferredSize(new java.awt.Dimension(75, 75));
        button01.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button01ActionPerformed(evt);
            }
        });

        button10.setPreferredSize(new java.awt.Dimension(75, 75));
        button10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button10ActionPerformed(evt);
            }
        });

        button11.setPreferredSize(new java.awt.Dimension(75, 75));
        button11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button11ActionPerformed(evt);
            }
        });

        button20.setPreferredSize(new java.awt.Dimension(75, 75));
        button20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button20ActionPerformed(evt);
            }
        });

        button12.setPreferredSize(new java.awt.Dimension(75, 75));
        button12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button12ActionPerformed(evt);
            }
        });

        button22.setPreferredSize(new java.awt.Dimension(75, 75));
        button22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button22ActionPerformed(evt);
            }
        });

        button21.setPreferredSize(new java.awt.Dimension(75, 75));
        button21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button21ActionPerformed(evt);
            }
        });

        labelJogador1.setText("Jogador 1:");

        labelJogador2.setText("Jogador 2:");

        labelEmpate.setText("Empates:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(button00, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button01, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button02, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(button10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(button11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(button20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(button21, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(button22, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                        .addComponent(buttonInicia)
                        .addContainerGap(22, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelJogador1)
                            .addComponent(labelJogadas)
                            .addComponent(labelJogador2)
                            .addComponent(labelEmpate))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button02, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button01, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button00, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonInicia)
                        .addGap(18, 18, 18)
                        .addComponent(labelJogadas)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(button11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(button21, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button22, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(labelJogador1)
                        .addGap(13, 13, 13)
                        .addComponent(labelJogador2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelEmpate)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonIniciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonIniciaActionPerformed
        campo = new Campo();
        jogada = new Jogada();
        button00.setText(null);
        button01.setText(null);
        button02.setText(null);
        button10.setText(null);
        button11.setText(null);
        button12.setText(null);
        button20.setText(null);
        button21.setText(null);
        button22.setText(null);
        button00.setEnabled(true);
        button01.setEnabled(true);
        button02.setEnabled(true);
        button10.setEnabled(true);
        button11.setEnabled(true);
        button12.setEnabled(true);
        button20.setEnabled(true);
        button21.setEnabled(true);
        button22.setEnabled(true);
        labelJogadas.setText(labelJ1);
    }//GEN-LAST:event_buttonIniciaActionPerformed

    
    private void button02ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button02ActionPerformed
        int valor = campo.getCampoValor(02);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(02, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button02.setText("X");
        if (vez == 1) button02.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button02ActionPerformed

    private void button00ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button00ActionPerformed
        int valor = campo.getCampoValor(00);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(00, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button00.setText("X");
        if (vez == 1) button00.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button00ActionPerformed

    private void button01ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button01ActionPerformed
        int valor = campo.getCampoValor(01);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(01, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button01.setText("X");
        if (vez == 1) button01.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button01ActionPerformed

    private void button10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button10ActionPerformed
        int valor = campo.getCampoValor(10);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(10, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button10.setText("X");
        if (vez == 1) button10.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button10ActionPerformed

    private void button11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button11ActionPerformed
        int valor = campo.getCampoValor(11);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(11, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button11.setText("X");
        if (vez == 1) button11.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button11ActionPerformed

    private void button12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button12ActionPerformed
        int valor = campo.getCampoValor(12);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(12, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button12.setText("X");
        if (vez == 1) button12.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button12ActionPerformed

    private void button20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button20ActionPerformed
        int valor = campo.getCampoValor(20);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(20, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button20.setText("X");
        if (vez == 1) button20.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button20ActionPerformed

    private void button21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button21ActionPerformed
        int valor = campo.getCampoValor(21);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(21, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button21.setText("X");
        if (vez == 1) button21.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button21ActionPerformed

    private void button22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button22ActionPerformed
        int valor = campo.getCampoValor(22);
        if(valor != 0) return;
        int vez = jogada.getNum();
        campo.alteraCampo(22, vez);
        labelJogadas.setText(labelJ1 + jogada.getJogada());
        if (vez == -1) button22.setText("X");
        if (vez == 1) button22.setText("O");
        if(campo.getVictorious() == true){
            if(vez == -1) Jog1++;
            if(vez == 1) Jog2++;
            wasVictorious();
        }
        else if (campo.campoCheio()) {
            Empate++;
            wasVictorious();
        }
    }//GEN-LAST:event_button22ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JogoDaVelhaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JogoDaVelhaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JogoDaVelhaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JogoDaVelhaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JogoDaVelhaGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button00;
    private javax.swing.JButton button01;
    private javax.swing.JButton button02;
    private javax.swing.JButton button10;
    private javax.swing.JButton button11;
    private javax.swing.JButton button12;
    private javax.swing.JButton button20;
    private javax.swing.JButton button21;
    private javax.swing.JButton button22;
    private javax.swing.JButton buttonInicia;
    private javax.swing.JLabel labelEmpate;
    private javax.swing.JLabel labelJogadas;
    private javax.swing.JLabel labelJogador1;
    private javax.swing.JLabel labelJogador2;
    // End of variables declaration//GEN-END:variables
}
